import 'package:data_plugin/bmob/bmob_query.dart';
import 'package:data_plugin/bmob/response/bmob_error.dart';
import 'package:data_plugin/bmob/response/bmob_saved.dart';
import 'package:data_plugin/bmob/table/bmob_installation.dart';
import 'package:data_plugin/bmob/table/bmob_object.dart';
import 'package:data_plugin/bmob/type/bmob_date.dart';
import 'package:data_plugin/bmob/type/bmob_file.dart';
/// CreateTime 2020/6/2 18:17
/// CreateUser chen 
/// ProjectName flutter_elf
import 'package:flutter_elf/config/component_index.dart';
import 'package:data_plugin/bmob/table/bmob_user.dart';
import 'package:flutter_elf/utils/toast_utils.dart';
import 'package:flutter_elf/widgets/common_widgets.dart';
class BmobFirstPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return BmobFirstPageState();
  }
}

class BmobFirstPageState extends State{
  MyBmobObject object = MyBmobObject(01, '陈晓青', 18);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarCreator.create(context, '后端云'),
      body: Column(
        children: <Widget>[
          buildButton('保存user', ()async{
            MyBmobObject object = MyBmobObject(01, '陈晓青', 18);
            BmobSaved saveRes = await object.save();
            print(saveRes.toJson());
          }),
          
          buildButton('查询信息', (){
            String id = 'b2dbef3682';
            queryAMessage(id);
          }),

          buildButton('修改信息', (){
            String id = '3f86ff8b4e';
            _updateSingle(id);
          }),

          buildButton('删除信息', (){
            String id = '40ee127138';
            _deleteSingle(id);
          }),

          buildButton('删除某个字段', (){
            String id = '49f6304922';
            _deleteFieldValue(id);
          }),

          buildButton('查询多条数据', (){
            _queryWhereEqual();
          }),

          buildButton('不等条件查询', (){
            _queryWhereNotEqual();
          }),
        ],
      ),
    );
  }

  ///不等条件查询
  void _queryWhereNotEqual() {
    BmobQuery<MyBmobObject> query = BmobQuery();
//    query.addWhereContains('iAge', '2');//多条件查询
    query.addWhereLessThan('iAge', 13);//多条件查询
    query.addWhereNotEqualTo("sName", "陈晓青");//多条件查询
    query.queryObjects().then((data) {
      showToast('成功');
      print(data);
    }).catchError((e) {
      showToast(BmobError.convert(e).error);
    });
  }

  ///等于条件查询
  void _queryWhereEqual() {
    BmobQuery<MyBmobObject> query = BmobQuery();
//    query.addWhereEqualTo("sName", "陈晓青");//去掉这句就是查询全部
    query.queryObjects().then((data) {
      print(data);
      showToast('查询成功');
    }).catchError((e) {
      showToast(BmobError.convert(e).error);
    });
  }

  //删除一个字段的值
  ///删除一个字段的值
  _deleteFieldValue(String currentObjectId) {
    if (currentObjectId != null) {
      MyBmobObject object = MyBmobObject.empty();
      object.objectId = currentObjectId;
      object.deleteFieldValue("sName").then((res) {
        showToast("删除sName内容成功：${res.updatedAt}");
      }).catchError((e) {
        showToast("删除sName内容失败" + BmobError.convert(e).error);
      });
    } else {
      showToast("请先新增一条数据");
    }
  }


  ///删除一条数据
  _deleteSingle(String currentObjectId) {
    if (currentObjectId != null) {
      MyBmobObject object = MyBmobObject.empty();
      object.objectId = currentObjectId;
      object.delete().then((re){

      }).catchError(((e){
        print(e.stackTrace());
        showToast(e.toString());
      }));
    } else {
      showToast("请先新增一条数据");
    }
  }

  ///修改一条数据
  _updateSingle(String currentObjectId) {
    if (currentObjectId != null) {
      MyBmobObject object = MyBmobObject(101,'呵呵哒',16);
      object.setObjectId(currentObjectId);
      object.update().then((res) {
        showToast("修改一条数据成功：${res.updatedAt}");
      }).catchError((e) {
        print(e.stackTrace());
        showToast( BmobError.convert(e).error);
      });
    } else {
      showToast( "请先新增一条数据");
    }
  }

  queryAMessage(String currentObjectId){
    ///查询一条数据
      if (currentObjectId != null) {
        //<MyBmobObject> 还要在MyBmobObject表中查询数据
        BmobQuery<MyBmobObject> bmobQuery = BmobQuery<MyBmobObject>();
        bmobQuery.setInclude("陈晓青");
        bmobQuery.queryObject(currentObjectId).then((data) {
          print(data);
//          showToast("查询一条数据成功：${blog.title} - ${blog.content} - ${blog.author.username}");
        }).catchError((e) {
          showToast(BmobError.convert(e).error);
        });
      } else {
        showToast("请先新增一条数据");
      }
  }

}

class MyBmobObject extends BmobObject{

  int iUserId;
  String sName;
  int iAge;
  Blog blog;
  MyBmobObject.empty();
  MyBmobObject(this.iUserId, this.sName, this.iAge);

  @override
  Map getParams() {
    // TODO: implement getParams
    Map<String ,dynamic> map = {
        'iUserId':this.iUserId,
        'sName':this.sName,
        'iAge':this.iAge,
        'objectId':objectId///update的时候，一定要写这个
    };
    return map;
  }
}


class Blog extends BmobObject{

  String title;
  String content;


  @override
  Map getParams() {
    // TODO: implement getParams
    Map<String,dynamic> map = {
      'title' : this.title,
      'content' : this.content,
      'objectId' : this.objectId,
    };
    return map;
  }
}

class User extends BmobObject{
  String sName;
  int iAge;
  bool bIsMale;
  @override
  Map getParams() {
    Map<String,dynamic> map = {
      "sName":this.sName,
      "iAge":this.iAge,
      "bIsMale":this.bIsMale,
      "objectId":"objectId",
    };
    return null;
  }
}